import { createSlice } from "@reduxjs/toolkit";
import { AppDispatch, RootState } from "store";
import * as auth from "auth-provider";
import { User } from "../screen/project-list/search-panel";
import { AuthForm } from "context/auth-context";
interface State {
  user: User | null;
}

const initialState: State = {
  user: null,
};

export const authSlice = createSlice({
  name: "auth",
  initialState,
  reducers: {
    setUser(state, action) {
      state.user = action.payload;
    },
  },
});
const { setUser } = authSlice.actions;
export const login = (form: AuthForm) => {
  return (dispatch: AppDispatch) => {
    auth.login(form).then((user) => {
      dispatch(setUser(user));
    });
  };
};
export const register = (form: AuthForm) => {
  return (dispatch: AppDispatch) => {
    auth.register(form).then((user) => {
      dispatch(setUser(user));
    });
  };
};
